package com.ruoyi.utils.alicloud;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.ruoyi.retirement.domain.RetirementConfig;
import com.ruoyi.retirement.service.IRetirementConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

import static com.aliyun.teautil.Common.assertAsString;

@Component
public class SmsUtil {
    @Autowired
    private IRetirementConfigService retirementConfigService;

    public static SmsUtil smsUtil;

    @PostConstruct
    public void init() {
        smsUtil = this;
        smsUtil.retirementConfigService = this.retirementConfigService ;
        // 初使化时将已静态化的otherService实例化
    }

    private static String ACCESS_KEY_ID = "";
    private static String ACCESS_KEY_SECRET = "";
    private static String ENDPOINT = "";
    private static String SIGN_NAME = "";
    private static String TEMPLATE_CODE = "";

    public static Client createClient() throws Exception {
        // 数据库查询最新的参数
        RetirementConfig retirementConfig = smsUtil.retirementConfigService.selectRetirementConfigById(1L);
        ACCESS_KEY_ID  = retirementConfig.getAccesskeyId();
        ACCESS_KEY_SECRET = retirementConfig.getAccesskeySecret();
        ENDPOINT = retirementConfig.getSmsEndpoint();
        SIGN_NAME = retirementConfig.getSmsSignName();
        TEMPLATE_CODE = retirementConfig.getSmsTemplateCode();

        Config config = new Config()
                // 配置 AccessKey ID
                .setAccessKeyId(ACCESS_KEY_ID)
                // 配置 AccessKey Secret
                .setAccessKeySecret(ACCESS_KEY_SECRET);
        // 配置 Endpoint 默认中国站点  dysmsapi.aliyuncs.com
        config.endpoint = ENDPOINT;
        return new Client(config);
    }

    /**
     * 发送验证码短信
     * @param phone
     * @param code
     */
    public static Map<String, Object> sendMsg(String phone, String code) throws Exception {
        // 所以构造Map类型的返回
        HashMap<String, Object> sendSmsResponseMap = new HashMap<>();
        try {
            // 初始化请求客户端
            Client client = SmsUtil.createClient();
            // 构造请求对象，请填入请求参数值
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(SIGN_NAME)
                    .setTemplateCode(TEMPLATE_CODE)
                    .setTemplateParam("{code:"+code+"}");
            // 获取响应对象
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            // 获取响应体状态码和Body状态码
            Integer statusCode = sendSmsResponse.getStatusCode();
            String responseCode = sendSmsResponse.getBody().getCode();
            sendSmsResponseMap.put("statusCode", statusCode);
            sendSmsResponseMap.put("bodyCode", responseCode);
        } catch (TeaException error) {
            // 这里要处理异常
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 这里要处理异常
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println("诊断地址" + error.getData().get("Recommend"));
            assertAsString(error.message);
        }
        return sendSmsResponseMap;
    }

}
